package com.wushi.lenovo.asia5b.app_ui.other.dialog.vm;

import android.app.Application;
import android.support.annotation.NonNull;

import com.mvp.base.BaseViewModel;
import com.mvp.base.model.ResultModel;
import com.wushi.lenovo.asia5b.app_http.ApiService;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;
import com.wushi.lenovo.asia5b.app_model.voucher.VoucherModel;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by zhangyuncai on 2019/11/13.
 */
public class SelectVoucherViewModel extends BaseViewModel {
    private ApiService apiService;

    public SelectVoucherViewModel(@NonNull Application application) {
        super(application);
        apiService = retrofit.create(ApiService.class);
    }

    /**
     * 获取优惠券列表
     *
     * @param resultModelConsumer 回调
     */
    public void getVoucherList(Consumer<ResultModel<List<VoucherModel>>> resultModelConsumer) {
        Disposable subscribe = apiService.grabGetVouchers(UserInfo.getMemberId())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe(disposable -> {
                    loadMvpView.showLoadingBar("");
                })
                .doAfterTerminate(() -> {
                    loadMvpView.dismissLoadingBar("");
                })
                .subscribe(resultModelConsumer, throwable -> {
                    throwable.printStackTrace();
                });
        composite.add(subscribe);
    }
}
